package com.github.gin.spring.boot.errorhandling.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by FSTMP on 2016/12/15.
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/public/**").addResourceLocations("classpath:/public/");
    }

    /**
     * 1、自定义ErrorViewResolver
     */
    /*public static class MyErrorViewResolver implements ErrorViewResolver {
        @Override
        public ModelAndView resolveErrorView(HttpServletRequest request,
                                             HttpStatus status, Map<String, Object> model) {
            // Use the request or status to optionally return a ModelAndView
            ModelAndView modelAndView = new ModelAndView("error/500",model);
            System.out.println(status.series());
            modelAndView.getModel().put("errorMsg","error");
            return modelAndView;
        }
    }

    @Bean
    public ErrorViewResolver errorViewResolver(){
        return new MyErrorViewResolver();
    }*/


    /**
     *  2、全局使用的以实现的HandlerExceptionResolver
     * @return
     */
   /*@Bean
   public HandlerExceptionResolver simpleMappingExceptionResolver(){
       SimpleMappingExceptionResolver handlerExceptionResolver = new SimpleMappingExceptionResolver();
       Properties properties = new Properties();
       properties.setProperty("java.lang.Throwable","error/500");
       handlerExceptionResolver.setExceptionMappings(properties);
       return handlerExceptionResolver;
   }*/

    /**
     *  3、全局使用自定义的HandlerExceptionResolver
     * @param exceptionResolvers
     */
    /*@Override
    public void extendHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
        exceptionResolvers.add(new HandlerExceptionResolver() {
            @Override
            public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
                ModelAndView modelAndView = new ModelAndView("error/500");
                return modelAndView;
            }
        });
    }*/

    /**
     *  4、使用@ExceptionHandler
     */
}
